<?php
namespace Api\Controller;
use JPush\Client as JPush;
class PushController extends ApiController {

    private  $app_key = 'd6bede8db785173f14856f13';

    private $master_secret = 'f607c96d28d2449120913361';


    public function pushOrder($title,$Notification,$Alias,$tag,$inv_to_id){
        $client = new JPush($this->app_key, $this->master_secret);
        try {
            $response = $client->push()
                ->setPlatform(array('ios', 'android'))
//                ->addTag(array($tag))
                  ->addAlias($Alias)
              //  ->setAudience('all')
//                ->setNotificationAlert('Hi, JPush')
                ->iosNotification($Notification, array(
                    'sound' => $Notification,
                    'badge' => 1,
                    'content-available' => true,
                    'category' => 'jiguang',
                    'extras' => array(
                        'key' => 'value',
                        'inv_to_id' => $inv_to_id,
                        'jiguang'
                    ),
                ))
               /* ->androidNotification('', array(
                    'title' => $title,
                   // 'build_id' => 2,
                    'extras' => array(
                        'key' => 'value',

                    ),
                ))*/
                ->message($Notification, array(
                    'title' => '',
                    'extras' => array(
                        'content_type' => 'text',
                        'inv_to_id' => $inv_to_id,
                        'code'=>'-96',
                        'jiguang'
                    ),
                ))
                ->options(array(
                 //   'time_to_live' => 86400,
                    'apns_production' => true,
                ))
                ->send();

        } catch (\JPush\Exceptions\APIConnectionException $e) {
            response(-2,array('msg'=>$e),'对方没有登录');
        } catch (\JPush\Exceptions\APIRequestException $e) {
            response(-2,array('msg'=>$e),'对方没有登录');
        }


        $report = $client->report();

        $rep = $report->getReceived($response['body']['msg_id']);
//            推送成功 更改状态
        if ($rep['http_code']==200)
        {
            return true;
        }else{
            return false;
        }
        //return true;
    }


    /**
     *查询推送消息
     *token
     */
    public function queryPushLog(){

        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);


        $log  = M('push_log');

        $map['uid'] = API_UID;

        $data = $log->field('meter_no,content,create_time')->where($map)->order('create_time desc')->select();

        empty($data)?response(-500):response(200,$data);

    }



    

}
?>